package com.atguigu.mvc.controller;

import com.atguigu.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@RequestMapping("/hello")
@Controller
public class HelloController {

    // @RequestMapping("/")
    @RequestMapping
    @ResponseBody
    public String index(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        /*// 响应端乱码 Content-Type: text/html;charset=ISO-8859-1
        System.out.println("contentType = " + response.getContentType());
        // # response.setContentType("Content-Type: text/html;charset=utf-8");
        // response.setHeader("Content-Type: text/html;charset=utf-8");

        response.setHeader("Content-Type", "text/html;charset=utf-8");  // 解放响应乱码
        response.setContentType("text/html;charset=utf-8");*/

        // 这里是 DispatcherServlet --> handler 已经经过了Servlet 此时设置编码问题, 为时已晚
        // 可以配置自己的 Filter,


        System.out.println("contentType = " + response.getContentType());

        System.out.println("request = " + request);
        System.out.println("请求方法 = " + request.getMethod());
        System.out.println("url = " + request.getRequestURL());


        StackTraceElement stackTraceElement = new Exception().getStackTrace()[0];
        return String.format("类名: %s, 方法名: %s", stackTraceElement.getClassName(), stackTraceElement.getMethodName());
    }

    @RequestMapping("/user")
    @ResponseBody
    public User showUser() {
        return new User(3, "阿勇", "深圳");
    }

    @RequestMapping("/view")
    public String showView() {
        // "FORWARD" dispatch for GET "/hello/01", parameters={}
        return "01";
    }

    // @RequestMapping注解：处理请求和控制器方法之间的映射关系
    // @RequestMapping注解的value属性可以通过请求地址匹配请求，/表示的当前工程的上下文路径
    // localhost:8080/springMVC/
    @RequestMapping("/index")
    public String index() {
        //设置视图名称
        return "index";
    }

}
